package com.manong.bookstore.search.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manong.common.utils.R;
import com.manong.bookstore.search.feign.ProduceFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27
 */

@RestController
@RequestMapping("search/produce")
public class ProduceController {
    // 查询商品的初始化方法
    @RequestMapping("/hello")
    public R hello(){
        return R.ok().put("search", "produce");
    }

    @Autowired
    private ProduceFeignService produceFeignService;

    // 调用远程服务produce的list方法
    // 展示调用produce的所有结果
    @RequestMapping("/produce/list")
    public R listProduce(){
        R list = produceFeignService.listProduce();
        return R.ok().put("produces", list);
    }

    // 对produce某一项的值进行升序或降序查询
    @RequestMapping("/produce/sequence")
    public R sequenceProduce(@RequestParam String key, @RequestParam(defaultValue = "true") Boolean isAsc){
        R list = produceFeignService.listProduce();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        if(!jsonArray.isEmpty()) {
            if(isAsc) {
                try {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getBigDecimal(key)));
                } catch (Exception e) {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString(key)));
                }
            } else {
                try {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getBigDecimal(key)).reversed());
                } catch (Exception e) {
                    jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString(key)).reversed());
                }
            }
        }
        int rows = jsonArray.size(); // 记录返回结果
        jsonObject.put("rows", rows);
        return R.ok().put("sequence", jsonObject);
    }

    // 对produce某一项的值进行精准查询
    @RequestMapping("/produce/accurate")
    public R accurateProduce(@RequestParam String key, @RequestParam String value){
        R list = produceFeignService.listProduce();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0; // 记录返回结果
        if(!jsonArray.isEmpty()) {
            try {
                jsonArray = jsonArray.stream().filter(
                    obj -> value.equals(((JSONObject) obj).getString(key))
                ).collect(Collectors.toCollection(JSONArray::new));
            } catch (Exception e) {
                jsonArray = jsonArray.stream().filter(
                    obj -> value.equals(((JSONObject) obj).getBigDecimal(key).toString())
                ).collect(Collectors.toCollection(JSONArray::new));
            }
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("accurate", jsonObject);
    }

    // 对produce某一项的值进行模糊查询
    @RequestMapping("/produce/fuzzy")
    public R fuzzyProduce(@RequestParam String key, @RequestParam String value){
        R list = produceFeignService.listProduce();
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get("page"));
        JSONArray jsonArray = jsonObject.getJSONArray("list");
        int rows = 0;
        if(!jsonArray.isEmpty()) {
            try {
                jsonArray = jsonArray.stream().filter(
                    obj -> (((JSONObject) obj).getString(key)).contains(value)
                ).collect(Collectors.toCollection(JSONArray::new));
            } catch (Exception e) {
                jsonArray = jsonArray.stream().filter(
                    obj -> ((JSONObject) obj).getBigDecimal(key).toString().contains(value)
                ).collect(Collectors.toCollection(JSONArray::new));
            }
            jsonObject.put("list", jsonArray);
            rows = jsonArray.size();
        } else {
            jsonObject.put("list", new JSONArray());
        }
        jsonObject.put("rows", rows);
        return R.ok().put("fuzzy", jsonObject);
    }

}
